package com.yearner.reviewplan.plan.model;

import com.google.gson.Gson;
import com.yearner.reviewplan.common.util.StringUtils;
import com.yearner.reviewplan.plan.commands.CreatePlanCommand;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * 创建plan，验证，分配id
 * @author pengyg
 * @since 2019/10/22
 */
@Component
public class PlanFactory {
    private static Gson GSON = null;
    static {
        GSON = new Gson();}
    public Plan generator(CreatePlanCommand command) {
        if (StringUtils.isBlank(command.getTitle())) {
            throw new RuntimeException("plan标题不能为空！");
        }
//        PlanId pid = PlanId.newPlanId(command.getUserId());
//        command.setPlanId(pid);
        if (command.getTag() == null) {
            command.setTag(new ArrayList<>());
        }
        Plan plan = new Plan();
        plan.setTag(GSON.toJson(command.getTag()));
        plan.setDetail(command.getDetail());
        plan.setDetailMd(command.getDetailMd());
        plan.setLastTime(System.currentTimeMillis());
        plan.setQuestion(command.getQuestion());
        plan.setQuestionList(command.getQuestionList());
        plan.setTitle(command.getTitle());
        plan.setLastTime(System.currentTimeMillis());
        return plan;
    }
}
